package gosu;

import com.sun.opengl.util.j2d.TextRenderer;
import java.awt.geom.Rectangle2D;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;


public class Font {
  private java.awt.Font _font;
  private Window _window;
  private TextRenderer _renderer;

  public Font(Window window, String fontName, int height) {
    _window = window;
    if (fontName.contains("/")) {
      try {
        InputStream is = Font.class.getResourceAsStream(fontName);
        if (is == null && new File(fontName).exists()) {
          is = new FileInputStream(new File(fontName));
        } else if (is == null) {
          throw new RuntimeException("Could not open file: " + fontName);
        }

        _font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is).deriveFont((float) height);
      } catch (RuntimeException e) { 
        throw e;
      } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
      }
    } else {
      _font = new java.awt.Font(fontName, java.awt.Font.BOLD, height);
    }
    _renderer = new TextRenderer(_font, true, false);
  }


  public int getHeight() { return _font.getSize(); }


  public void draw(String text, double x, double y, double z) {
    _window.addDrawOp(new FontDrawOp(_window, _renderer, text, x, y, z));
  }


  public void draw(String text, double x, double y, double z, double factorX, double factorY) {
    _window.addDrawOp(new FontDrawOp(_window, _renderer, text, x, y, z, factorX, factorY));
  }


  public void draw(String text, double x, double y, double z, double factorX, double factorY, Color c, int mode) {
    _window.addDrawOp(new FontDrawOp(_window, _renderer, text, x, y, z, factorX, factorY, c, mode));
  }


  public void drawRot(String text, double x, double y, double z, double angle) {
    FontDrawOp op = new FontDrawOp(_window, _renderer, text, x, y, z);
    op.setAngle(angle);
    _window.addDrawOp(op);
  }


  public void drawRot(String text, double x, double y, double z, double angle, double factorX, double factorY) {
    FontDrawOp op = new FontDrawOp(_window, _renderer, text, x, y, z, factorX, factorY);
    op.setAngle(angle);
    _window.addDrawOp(op);
  }


  public void drawRot(String text, double x, double y, double z, double angle, double factorX, double factorY, Color c, int mode) {
    FontDrawOp op = new FontDrawOp(_window, _renderer, text, x, y, z, factorX, factorY, c, mode);
    op.setAngle(angle);
    _window.addDrawOp(op);
  }


  public void drawRel(String text, double x, double y, double z, double relX, double relY) {
    Rectangle2D bounds = _renderer.getBounds(text);
    x = x - (relX * bounds.getWidth());
    y = y - (relY * getHeight());
    _window.addDrawOp(new FontDrawOp(_window, _renderer, text, x, y, z));
  }


  public void drawRel(String text, double x, double y, double z, double relX, double relY, double factorX, double factorY) {
    Rectangle2D bounds = _renderer.getBounds(text);
    x = x - (relX * factorX * bounds.getWidth());
    y = y - (relY * factorY * getHeight());
    _window.addDrawOp(new FontDrawOp(_window, _renderer, text, x, y, z, factorX, factorY));
  }


  public void drawRel(String text, double x, double y, double z, double relX, double relY, double factorX, double factorY, Color c, int mode) {
    Rectangle2D bounds = _renderer.getBounds(text);
    x = x - (relX * factorX * bounds.getWidth());
    y = y - (relY * factorY * getHeight());
    _window.addDrawOp(new FontDrawOp(_window, _renderer, text, x, y, z, factorX, factorY, c, mode));
  }


  public double textWidth(String text) {
    // For a reason beyond my frail and mortal understanding,
    // the TextRenderer does not care about trailing or leading whitespace.
    // I'm the only one that cares.

    return _renderer.getBounds(text).getWidth() + ((text.length() - text.trim().length()) * _renderer.getCharWidth(' '));
  }
}
